package liangtWorkSpace.stream;

/**
 * @ClassName StreamTest1
 * @Description : 功能说明
 * @Author : LiangT
 * @Date : 2020/4/22 11:35
 */

import java.util.*;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;

/**
 * java7 和java8对于几个遍历迭代方式
 */
public class StreamTest1 {


    /**
     * java7对于集合的迭代方式
     */
    public static void java7(List<Dish> dishes) {
        List<Dish> list = dishes;
        /**
         * 输出排序前菜品信息
         */
        for (Dish s : list) {
            System.out.println("s:" + s);
        }
        /**
         * 按照菜单序号对菜品进行排序
         */
        Collections.sort(list, new Comparator<Dish>() {
            @Override
            public int compare(Dish o1, Dish o2) {
                return Integer.compare(o1.getCalories(), o2.getCalories());
            }
        });
        /**
         * 输出排序后菜品信息
         */
        for (Dish i : list) {
            System.out.println("i:" + i);
        }
    }

    /**
     * 以java8形式对集合进行迭代
     */
    public static void java8(List<Dish> dishes) {
        List<Dish> list = dishes;
        /**
         * 使用流的方式对list集合进行遍历
         */
        List<String> dishList = list.stream()
                .filter(d -> d.getCalories() < 400) //挑选出菜品序号在400以上的
                .sorted(comparing(Dish::getCalories))//按照菜品序号进行排序
                .map(Dish::getName)//获取菜品名称
                .collect(toList());//转化为list
        dishList.stream().forEach((d)-> {
            System.out.println(d);;
        });
//        System.out.println(Arrays.toString(dishList.toArray()));
        /**
         * 以并行流的方式对list集合进行迭代
         */
        List<String> listP = list.parallelStream()
                .filter(d -> d.getCalories() < 400)
                .sorted(comparing(Dish::getCalories))
                .map(Dish::getName)
                .collect(toList());

        listP.parallelStream().forEach(System.out::printf);
    }

    public static void main(String[] args) {
        List<Dish> menu = Arrays.asList(
                new Dish("pork", false, 800, Dish.Type.MEAT),
                new Dish("beef", false, 700, Dish.Type.MEAT),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("french fries", true, 530, Dish.Type.OTHER),
                new Dish("rice", true, 350, Dish.Type.OTHER),
                new Dish("season fruit", true, 120, Dish.Type.OTHER),
                new Dish("pizza", true, 550, Dish.Type.OTHER),
                new Dish("prawns", false, 300, Dish.Type.FISH),
                new Dish("salmon", false, 450, Dish.Type.FISH));
        System.out.println("**************************基础List信息************************");
        System.out.println(Arrays.toString(menu.toArray()));
        System.out.println("**************************java7List信息************************");
        java7(menu);
        System.out.println("**************************java8List信息************************");
        java8(menu);
    }


}

class Dish {
    private String name;//菜名
    private boolean mane;
    private int calories;//菜單排序
    private Type type;

    public Dish(String name, boolean mane, int calories, Type type) {
        this.name = name;
        this.mane = mane;
        this.calories = calories;
        this.type = type;
    }

    @Override
    public String toString() {
        return "Dish{" +
                "name='" + name + '\'' +
                ", mane=" + mane +
                ", calories=" + calories +
                ", type=" + type +
                '}';
    }

    public int getCalories() {
        return calories;
    }

    public void setCalories(int calories) {
        this.calories = calories;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isMane() {
        return mane;
    }

    public void setMane(boolean mane) {
        this.mane = mane;
    }


    enum Type {
        MEAT,
        OTHER,
        FISH;
    }
}

/**
 * 对List进行迭代循环遍历的三种实现方式
 */
class ForEach{
    public static void main(String[] args) {
        List<String> strings = new ArrayList<>();

    }

    /**
     * 使用for-each迭代
     * @param list
     */
    public static void forEach(List<String> list){
        for (String s: list){
            System.out.println(s);
        }
    }

    /**
     * 使用Iterator完成迭代
     * @param list
     */
    public static void interator(List<String> list){
        Iterator <String> iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    /**
     * 使用流进行迭代
     * @param list
     */
    public static void stream(List<String> list){
        list.parallelStream().forEach(System.out::printf);
    }
}
